package com.fractal;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class MandelView extends SurfaceView implements SurfaceHolder.Callback{
	
	private MandelGenerator generator;
    private Bitmap img;  
    private Canvas canvas;
    private GetDatabase bookmarks;
    
	private float sx;
    private float sy;
    private float dx;
    private float dy;
    
    public MandelView(Context context, AttributeSet attrs)
    {
    	super(context, attrs);
        getHolder().addCallback(this);
    }
    
    @Override
    public void onDraw(Canvas canvas) 
    { 
    	super.onDraw(canvas);
    	canvas.drawColor(Color.BLACK);
    	canvas.drawBitmap(img, dx, dy, null);
    }
    
    public void updateDrag()
    {
		canvas = getHolder().lockCanvas(null);
		try
		{
			if (canvas != null)
				synchronized (getHolder()) 
				{
					onDraw(canvas);
				}
		}
        finally 
        {
            if (canvas != null) 
            {
            	getHolder().unlockCanvasAndPost(canvas);
            	canvas = null;
            }
        }    				

    }
    
    public void setBookmarks(GetDatabase db)
    {
    	bookmarks = db;
    	if (generator != null)
    	{
    		generator.setBookmarks(db);
    		bookmarks = null;
    	}
    	
    }
    
    public void setPalette (int type)
    {
    	generator.setPalette(type);
    }
    
    public void zoomIn()
    {
    	generator.zoom(0.5);
    }
    
    public void zoomOut()
    {
    	generator.zoom(2d);
    }
    
    public void setMaxIteration(short i)
    {
    	generator.setMaxIterations(i);
    }
    
    public void reset()
    {
    	generator.reset(null);
    }
    
    public void stopThread()
    {
    	generator.stopThread();
    }
    
    public ArrayList<Double> getInfo()
    {
    	return generator.getInfo();
    }
    
    public void render()
    {
    	generator.render();
    }
    
    public Bitmap getImg()
    {
    	return img;
    }
    
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){}

	@Override
	public void surfaceCreated(SurfaceHolder holder) 
	{
		if (img == null)
			img = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888);

		if (generator == null)
		{
			generator = new MandelGenerator(this.getWidth(), this.getHeight(), getHolder(), img);
			generator.reset(bookmarks);
			bookmarks = null;
		}
		Log.d("profile", "Start");
		generator.startThread();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) 
	{
		generator.stopThread();
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) 
	{
		if (event.getAction() == MotionEvent.ACTION_DOWN) 
		{
			Log.d("debug","Push: " + event.getX() + ", " + event.getY());
			generator.pauseThread();
        	sx = event.getX();
        	sy = event.getY();
        } 
		else if (event.getAction() == MotionEvent.ACTION_MOVE)
        {
        	dx += event.getX()-sx;
        	dy += event.getY()-sy;
        	sx = event.getX();
        	sy = event.getY();
        	updateDrag();
        } 
		else if (event.getAction() == MotionEvent.ACTION_UP) 
		{
			Log.d("mouse","Pull: " + event.getX() + ", " + event.getY());
			Log.d("mouse","Pull: " + dx + ", " + dy);
			generator.translate(dx, dy);
			dx = dy = 0;
        }
        return true;
	} 
}	
